package com.fakeDYChat.common.user.controller;


import com.fakeDYChat.common.common.annotation.RedissonLock;
import com.fakeDYChat.common.common.domain.vo.resp.ApiResult;
import com.fakeDYChat.common.common.utils.AssertUtil;
import com.fakeDYChat.common.common.utils.RequestHolder;
import com.fakeDYChat.common.user.domain.enums.RoleEnum;
import com.fakeDYChat.common.user.domain.vo.resp.*;
import com.fakeDYChat.common.user.service.IRoleService;
import com.fakeDYChat.common.user.service.IUserRoleService;
import com.fakeDYChat.common.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Tang
 * @since 2023-11-18
 */
@RestController
@Api(tags = "用户行为接口")
@RequestMapping("/capi/user")
public class UserController {
    private final IUserService iUserService;

    private final IRoleService iRoleService;

    public UserController(IUserService iUserService, IRoleService iRoleService) {
        this.iUserService = iUserService;
        this.iRoleService = iRoleService;
    }

    @GetMapping("/userInfo")
    @ApiOperation(value = "获取用户信息")
    public ApiResult<UserInfoResp> getUserInfo() {
        return ApiResult.success(iUserService.getUserInfo(RequestHolder.get().getUid()));
    }

    @PostMapping("/name")
    @ApiOperation(value = "修改用户名")
    public ApiResult<Void> modifyUserName(@RequestBody @Validated ModifyName modifyName) {
        iUserService.modifyName(RequestHolder.get().getUid(), modifyName.getName());
        return ApiResult.success();
    }

    @GetMapping("/badge")
    @ApiOperation(value = "查询所有徽章，且能标明用户所佩戴徽章的列表")
    public ApiResult<List<BadgeResp>> badges() {
        return ApiResult.success(iUserService.badges(RequestHolder.get().getUid()));
    }

    @PutMapping("/badge")
    @ApiOperation(value = "用户佩戴徽章")
    public ApiResult<Void> wearingBadge(@RequestBody @Validated WearingBadgeReq req) {
        iUserService.wearingBadge(RequestHolder.get().getUid(), req);
        return ApiResult.success();
    }

    @PutMapping("/black")
    @ApiOperation(value = "拉黑用户")
    public ApiResult<Void> blackUser(@RequestBody @Validated BlackResp req) {
        Long uid = RequestHolder.get().getUid();
        boolean hasPower = iRoleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(hasPower, "暂无权限");
        iUserService.black(req);
        return ApiResult.success();
    }

}

